from appium.webdriver.common.appiumby import AppiumBy
from selenium.common import TimeoutException, NoSuchElementException
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from common.report_add_img import add_img_2_report


class Base:
    def find_element_new(self,driver,locator_type,locator_value,timeout=10):
        try:
            element = WebDriverWait(driver,timeout).until(
                EC.presence_of_element_located((locator_type,locator_value))
            )
            return element
        except(TimeoutException,NoSuchElementException):
            print(f"元素未找到:{locator_type},{locator_value}")
            return None

    def send_keys_new(self, driver, locator_type, locator_value,value,timeout=10):
        try:
            element = self.find_element_new(driver,locator_type,locator_value,timeout)
            element.clear()
            element.send_keys(value)
        except Exception as e:
            print(f"元素未找到:{locator_type,locator_value}中输入内容失败：{str(e)}")



    def click_new(self,driver,locator_type,locator_value,timeout=10):
         try:
             element = self.find_element_new(driver, locator_type, locator_value, timeout)
             element.click()
         except Exception as e:
             print(f"点击元素{locator_value,locator_type}失败：{str(e)}")

    def get_text(self,driver,locator_type=AppiumBy.XPATH,locator_value=None):
        element = self.find_element_new(driver,locator_type,locator_value)
        try:
            element_text = element.text
            return element_text
        except Exception as e:
            print(f'元素text获取失败，错误信息为{e}')

class BaseAssert:
    def assert_equal_screen_shot(self,driver,exceptor,step_name):
        if not isinstance(exceptor,tuple):
            print('exceptor类型传入错误，必须要传入元祖类型')
        else:
            try:
                print(f'正在断言{exceptor[0]}s是否等于{exceptor[1]}')
                assert exceptor[0] == exceptor[1]
                add_img_2_report(driver,step_name=step_name)
            except Exception as e:
                print(f'断言失败：{str(e)}')
                add_img_2_report(driver,step_name=step_name)
                raise e







